<?php
namespace App\Utils;
use Exception;


class Helper
{
    static public function generateTree($items , $pId): array
    {
        $tree = [];
        foreach($items as $k => $v)
        {
            if($v['pid'] == $pId)
            {
                if (!empty(self::generateTree($items , $v['aid'])))
                {
                    $v['children'] = self::generateTree($items , $v['aid']);
                }
                $tree[] = $v;
            }
        }
        return $tree;
    }

    static public function deleteDirectory($dirPath) {
        if (!is_dir($dirPath)) {
            throw new Exception("$dirPath 必须是一个目录");
        }
     
        $files = array_diff(scandir($dirPath), array('.', '..'));
        foreach ($files as $file) {
            $path = $dirPath . '/' . $file;
            if (is_dir($path)) {
                self::deleteDirectory($path);
            } else {
                unlink($path);
            }
        }
     
        return rmdir($dirPath);
    }

    static function cm2in($cm){
        return round($cm * 0.39,4);
    }

    static function kg2ln($cm){
        return round($cm * 2.2,4);
    }
}